package com.jsmile.cloud.tradecenter.order.model;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.jsmile.cloud.goodscenter.api.res.ResSku;
import com.jsmile.cloud.goodscenter.api.res.ResSkuSpec;
import com.jsmile.cloud.tradecenter.api.req.ReqOrderCreate;
import com.jsmile.mall.api.kit.StrKit;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2019年12月19日 02:56:26
 */
@ApiModel
@Data
@TableName("t_order_product")
public class OrderProduct extends Model<OrderProduct> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "id")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    @ApiModelProperty(value = "用户id")
    private Long userId;
    @ApiModelProperty(value = "订单编号")
    private Long orderNo;
    @ApiModelProperty(value = "sku_id")
    private Long skuId;
    @ApiModelProperty(value = "商品id")
    private Long spuId;
    @ApiModelProperty(value = "商品名称")
    private String productName;
    @ApiModelProperty(value = "主图")
    private String productImage;
    @ApiModelProperty(value = "商品价格")
    private Long sellingPrice;
    @ApiModelProperty(value = "成本价")
    private Long costPrice;
    @ApiModelProperty(value = "购买时的价格")
    private Long buyPrice;
    @ApiModelProperty(value = "平台价")
    private Long platformPrice;
    @ApiModelProperty(value = "市场价")
    private Long marketPrice;
    @ApiModelProperty(value = "券后价")
    private Long couponPrice;
    @ApiModelProperty(value = "所参加的限时活动的 id")
    private Long activityId;
    @ApiModelProperty(value = "单品活动价格")
    private Long activityPrice;
    @ApiModelProperty(value = "规格json")
    private String specs;
    @ApiModelProperty(value = "购买数量")
    private Integer productNumber;
    @ApiModelProperty(value = "商品参加活动的数量")
    private Integer activityNumber;
    @ApiModelProperty(value = "商品总金额")
    private Long productAmount;
    @ApiModelProperty(value = "实际购买价格")
    private Long actuallyPayAmount;
    @ApiModelProperty(value = "余额抵扣金额")
    private Long balanceAmount;
    @ApiModelProperty(value = "折扣金额:优惠券,活动,积分抵扣之和")
    private Long discountAmount;
    @ApiModelProperty(value = "优惠券金额")
    private Long ticketDisamt;
    @ApiModelProperty(value = "活动优惠金额")
    private Long promotionDisamt;
    @ApiModelProperty(value = "积分抵扣金额")
    private Long pointDisamt;
    @ApiModelProperty(value = "create_time")
    private Date createTime;
    @ApiModelProperty(value = "更新时间")
    private Date updateTime;
    @ApiModelProperty(value = "del_flag")
    @TableLogic
    private Integer delFlag;
    @ApiModelProperty(value = "tenant_id")
    private Long tenantId;
    @ApiModelProperty(value = "平台id")
    private Integer platformId;
    @ApiModelProperty(value = "业务id")
    private Integer bizCode;
    @ApiModelProperty(value = "备注")
    private String remark;
    @ApiModelProperty(value = "退款标记（0未退款，1退款）")
    private Integer refundFlag;
    @ApiModelProperty(value = "退款时间")
    private Date refundTime;
    @TableField(exist = false)
    @ApiModelProperty(value = "规格信息")
    private List<ResSkuSpec> skuSpecs;

    public static List<OrderProduct> create(Order order, List<ReqOrderCreate.OrderSku> orderSkus, Map<Long, ResSku> skuMap) {
        return orderSkus.stream().map(x -> {
            OrderProduct orderProduct = BeanUtil.toBean(x, OrderProduct.class);
            orderProduct.setOrderNo(order.getId());
            orderProduct.setSellingPrice(skuMap.get(x.getSkuId()).getSellingPrice());
            orderProduct.setCostPrice(skuMap.get(x.getSkuId()).getCostPrice());
            orderProduct.setCouponPrice(skuMap.get(x.getSkuId()).getCouponPrice());
            orderProduct.setMarketPrice(skuMap.get(x.getSkuId()).getMarketPrice());
            orderProduct.setPlatformPrice(skuMap.get(x.getSkuId()).getPlatformPrice());
            orderProduct.setProductName(skuMap.get(x.getSkuId()).getSpuInfo().getSpuName());
            orderProduct.setProductImage(skuMap.get(x.getSkuId()).getSpuInfo().getImageUrl());
            orderProduct.setProductNumber(x.getSkuNumber());
            orderProduct.setProductAmount(x.getSkuNumber() * orderProduct.getSellingPrice());
            orderProduct.setUserId(order.getUserId());
            orderProduct.setCreateTime(new Date());
            orderProduct.setProductImage(skuMap.get(x.getSkuId()).getImageUrl());
            if (StrKit.isBlank(orderProduct.getProductImage())) {
                orderProduct.setProductImage(skuMap.get(x.getSkuId()).getSpuInfo().getImageUrl());
            }
            if (!CollectionUtils.isEmpty(skuMap.get(x.getSkuId()).getSkuSpecs())) {
                orderProduct.setSpecs(JSON.toJSONString(skuMap.get(x.getSkuId()).getSkuSpecs()));
            }
            orderProduct.setSkuSpecs(skuMap.get(x.getSkuId()).getSkuSpecs());
            orderProduct.setBuyPrice(skuMap.get(x.getSkuId()).getSellingPrice() * x.getSkuNumber());
            orderProduct.setBalanceAmount(0L);
            orderProduct.setTicketDisamt(0L);
            orderProduct.setDiscountAmount(0L);
            orderProduct.setPointDisamt(0L);
            orderProduct.setPromotionDisamt(0L);
            orderProduct.setActuallyPayAmount(
                orderProduct.getBuyPrice() - orderProduct.getBalanceAmount() - orderProduct.getDiscountAmount() - orderProduct.getPointDisamt() - orderProduct.getTicketDisamt());
            return orderProduct;
        }).collect(Collectors.toList());
    }
}
